package org.yzh.client;

import io.github.yezhihao.netmc.core.annotation.Endpoint;
import io.github.yezhihao.netmc.core.annotation.Mapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yzh.protocol.RealtimeInfo;
import org.yzh.protocol.VehicleLogin;
import org.yzh.protocol.basics.GTMessage;
import org.yzh.protocol.commons.GTID;
import org.yzh.web.endpoint.JT808Endpoint;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yezhihao
 * @home https://gitee.com/yezhihao/jt808-server
 */
@Endpoint
public class JTClientEndpoint {

    private static final Logger log = LoggerFactory.getLogger(JT808Endpoint.class.getSimpleName());

    private AtomicInteger serialNo = new AtomicInteger();

    private String mobileNo = "12345678901";

    @Mapping(types = 平台通用应答, desc = "平台通用应答")
    public void 平台通用应答(VehicleLogin message) {
    }

    @Mapping(types = 服务器补传分包请求, desc = "服务器补传分包请求")
    public VehicleLogin 服务器补传分包请求(T8003 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 查询服务器时间应答, desc = "查询服务器时间应答")
    public VehicleLogin 查询服务器时间应答(T8004 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 终端注册应答, desc = "终端注册应答")
    public VehicleLogin 终端注册应答(T8100 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 设置终端参数, desc = "设置终端参数")
    public VehicleLogin 设置终端参数(T8103 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 查询终端参数, desc = "查询终端参数")
    public VehicleLogin 查询终端参数(GTMessage message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 终端控制, desc = "终端控制")
    public VehicleLogin 终端控制(T8105 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 查询指定终端参数, desc = "查询指定终端参数")
    public VehicleLogin 查询指定终端参数(T8106 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 查询终端属性, desc = "查询终端属性")
    public VehicleLogin 查询终端属性(T0107 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 下发终端升级包, desc = "下发终端升级包")
    public VehicleLogin 下发终端升级包(T8108 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 位置信息查询, desc = "位置信息查询")
    public RealtimeInfo 位置信息查询(GTMessage message) {
        RealtimeInfo result = new RealtimeInfo();
        //TODO
        return result;
    }

    @Mapping(types = 临时位置跟踪控制, desc = "临时位置跟踪控制")
    public VehicleLogin 临时位置跟踪控制(T8202 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 人工确认报警消息, desc = "人工确认报警消息")
    public VehicleLogin 人工确认报警消息(T8203 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 服务器向终端发起链路检测请求, desc = "服务器向终端发起链路检测请求")
    public VehicleLogin 服务器向终端发起链路检测请求(GTMessage message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 文本信息下发, desc = "文本信息下发")
    public VehicleLogin 文本信息下发(T8300 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 事件设置, desc = "事件设置")
    public VehicleLogin 事件设置(T8301 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 提问下发, desc = "提问下发")
    public VehicleLogin 提问下发(T8302 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 信息点播菜单设置, desc = "信息点播菜单设置")
    public VehicleLogin 信息点播菜单设置(T8303 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 信息服务, desc = "信息服务")
    public VehicleLogin 信息服务(T8304 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 电话回拨, desc = "电话回拨")
    public VehicleLogin 电话回拨(T8400 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 设置电话本, desc = "设置电话本")
    public VehicleLogin 设置电话本(T8401 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 车辆控制, desc = "车辆控制")
    public VehicleLogin 车辆控制(T8500 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = {删除圆形区域, 删除矩形区域, 删除多边形区域, 删除路线,}, desc = "删除圆形区域")
    public VehicleLogin 删除圆形区域(T8601 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 设置圆形区域, desc = "设置圆形区域")
    public VehicleLogin 设置圆形区域(T8600 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }


    @Mapping(types = 设置矩形区域, desc = "设置矩形区域")
    public VehicleLogin 设置矩形区域(T8602 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 设置多边形区域, desc = "设置多边形区域")
    public VehicleLogin 设置多边形区域(T8604 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 设置路线, desc = "设置路线")
    public VehicleLogin 设置路线(T8606 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 查询区域或线路数据, desc = "查询区域或线路数据")
    public VehicleLogin 查询区域或线路数据(T8608 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 行驶记录仪数据采集命令, desc = "行驶记录仪数据采集命令")
    public VehicleLogin 行驶记录仪数据采集命令(GTMessage message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 行驶记录仪参数下传命令, desc = "行驶记录仪参数下传命令")
    public VehicleLogin 行驶记录仪参数下传命令(T8701 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 上报驾驶员身份信息请求, desc = "上报驾驶员身份信息请求")
    public VehicleLogin 上报驾驶员身份信息请求(GTMessage message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 多媒体数据上传应答, desc = "多媒体数据上传应答")
    public VehicleLogin 多媒体数据上传应答(T8800 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 摄像头立即拍摄命令, desc = "摄像头立即拍摄命令")
    public VehicleLogin 摄像头立即拍摄命令(T8801 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 存储多媒体数据检索, desc = "存储多媒体数据检索")
    public VehicleLogin 存储多媒体数据检索(T8802 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 存储多媒体数据上传, desc = "存储多媒体数据上传")
    public VehicleLogin 存储多媒体数据上传(T8803 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 录音开始命令, desc = "录音开始命令")
    public VehicleLogin 录音开始命令(T8804 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 单条存储多媒体数据检索上传命令, desc = "单条存储多媒体数据检索上传命令")
    public VehicleLogin 单条存储多媒体数据检索上传命令(T8805 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 数据下行透传, desc = "数据下行透传")
    public VehicleLogin 数据下行透传(T8900 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    @Mapping(types = 平台RSA公钥, desc = "平台RSA公钥")
    public VehicleLogin 平台RSA公钥(T06 message) {
        VehicleLogin result = buildResult(message, VehicleLogin.Success);
        return result;
    }

    private VehicleLogin buildResult(GTMessage message, int resultCode) {
        VehicleLogin result = new VehicleLogin();
        result.copyBy(message);
        result.setMessageId(GTID.终端通用应答);
        result.setClientId(mobileNo);
        result.setSerialNo(serialNo.addAndGet(1));

        result.setResponseSerialNo(message.getSerialNo());
        result.setResponseMessageId(message.getMessageId());
        result.setResultCode(resultCode);
        return result;
    }
}